package cn.gailvlun.gll.util;

import org.joda.time.DateTime;
import org.joda.time.Period;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;



public class DateUtil {
    public static String NORMAL_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static String DATE_PATTERN = "yyyy-MM-dd";
    public static String NO_SECOND_PATTERN = "yyyy-MM-dd HH:mm";
    public static String MESSAGE_PATTERN = "MM.dd HH:mm";
    public static String HOUR_MINUTE_PATTERN = "HH:mm";
    public static String ONLY_DAY_PATTERN = "dd";
    public static String DOT_NO_SECOND_PATTERN = "yyyy.MM.dd HH:mm";
    public static String SERVER_PATTERN = "yyyy-MM-dd'T'HH:mm:ssX";
    public static String TASK_PATTERN = "HH:mm:ss";
    public static String NO_YEAR_SECONDS_PATTERN = "MM-dd HH:mm";

    public static String format(Date date) {
        return format(NORMAL_PATTERN, date);
    }

    public static String format(String pattern, Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.getDefault());
        return sdf.format(date);
    }

    public static Date parse(String date) {
        return parse(NORMAL_PATTERN, date);
    }

    public static Date parse(String pattern, String date) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.getDefault());
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Calendar toCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static boolean isInFiveMinute(Date firstDate, Date secondDate) {
        long diff =  Math.abs(secondDate.getTime() - firstDate.getTime());
        return diff < 5 * 60 * 1000;
    }

    public static String toBirthday(Calendar birthday) {
        return format(DATE_PATTERN, birthday.getTime());
    }

    
    public static String getAge(String birthday) {
        if (birthday == null) {
            return null;
        }
        Period p = new Period(new DateTime(DateUtil.parse(DateUtil.DATE_PATTERN, birthday)), DateTime.now());
        return String.valueOf(p.getYears());
    }

    
    public static String getChatTime(Date time) {
        Date now = new Date();
        if (time.getYear() == now.getYear()) {
            if (time.getMonth() == now.getMonth() &&
                    time.getDay() == now.getDay()) {
                return DateUtil.format(HOUR_MINUTE_PATTERN, time);
            } else {
                return DateUtil.format(NO_YEAR_SECONDS_PATTERN, time);
            }
        } else {
            return DateUtil.format(NO_SECOND_PATTERN, time);
        }
    }

    public static boolean isSameDay(DateTime d1, DateTime d2) {
        return d1.getYear() == d2.getYear() && d1.getDayOfYear() == d2.getDayOfYear();
    }
}
